#CXX = g++
CXX = mpic++ -DUSE_IRIS
UARCH_DIR=../../uarch
CPPFLAGS = -I. -I../../kernel/include -I$(UARCH_DIR)/../ -I../../models -I../../models/cache -I../../models/network -I../../models/processor -I../../models/memory -I../../models/network/iris
DBG = -g
LINKER = $(CXX) -ldl 
EXTRA_LIBS=./../../kernel/src/libmanifold.a ./../../models/network/iris/libiris.a \
	   ./../../models/processor/CaffProc/libCaffProc.a ../../models/cache/CaffCache/libCaffCache.a \
	   ../../models/memory/CaffMC/libCaffMC.a #../../models/network/CaffNet/libCaffNet.a

# our macs use g++ 4.2.1, pasta uses g++ 4.1.2
# compilation will die because of a deprecated conversion from string
# constant to char* error
#CXXFLAGS = -O0 $(DBG) -Wall -Werror -Wno-unknown-pragmas -fno-strict-aliasing
CXXFLAGS = $(DBG) -Wall -fno-strict-aliasing -m64 -lpthread -DUSE_IRIS -L./../../kernel/src -lmanifold #-L ./processor/zesto -lZesto # -lqsim -I../distorm/ -I/usr/local/include

SOURCES:= \
	$(UARCH_DIR)/node.cpp \
	$(UARCH_DIR)/module.cpp \
	$(UARCH_DIR)/settings.cpp \
	$(UARCH_DIR)/signals.cpp \
	$(UARCH_DIR)/sim.cpp \
	$(UARCH_DIR)/sim_stat_manager.cpp \
	$(UARCH_DIR)/stat_engine.cpp \
	$(UARCH_DIR)/ticker.cpp  \

SOURCES_EXTRA:= \
	$(UARCH_DIR)/node_to_iris_module.cpp \
	$(UARCH_DIR)/node_terminal.cpp  \
	$(UARCH_DIR)/mreq.cpp 

  #./frontend/emulator_interface.cpp\
  #./frontend/thread.cpp
	#../../models/processor/preq.cpp

#	./processor/insn.cpp\
#	./processor/simple/preq.cpp\
#	./processor/simple/processor.cpp\
#	./processor/simple/processor_context.cpp\
#	./processor/simple/processor_simple.cpp
#	./processor/preq.cpp\

SOURCES_ALL:= $(SOURCES) $(SOURCES_EXTRA)

HEADERS:=$(patsubst %.cpp, %.h, $(SOURCES_ALL))
OBJECTS:=$(patsubst %.cpp, %.o, $(SOURCES_ALL))
DEPS:=$(patsubst %.cpp, %.d, $(SOURCES))

all: clean_bin settings $(DEPS) sim_trace
deps: $(DEPS)

%.d: %.cpp
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -MM $< > $@ 

include $(wildcard *.d)

settings: $(UARCH_DIR)/settings.l $(UARCH_DIR)/settings.y
	flex -o$(UARCH_DIR)/lex.yy.c $(UARCH_DIR)/settings.l 
	bison -o$(UARCH_DIR)/settings.tab.c -d $(UARCH_DIR)/settings.y
	$(CXX) $(CPPFLAGS) -o$(UARCH_DIR)/settings.tab.o -c $(UARCH_DIR)/settings.tab.c
	$(CXX) $(CPPFLAGS) -o$(UARCH_DIR)/lex.yy.o -c $(UARCH_DIR)/lex.yy.c


%.o: %.cpp 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o ${OUTOPT} $@

sim_trace: $(DEPS) $(OBJECTS) ./main.o ./sim_trace.o ./sim_trace.h $(UARCH_DIR)/settings.l $(UARCH_DIR)/settings.y
	${LINKER} $(CXXFLAGS) ./main.o ./sim_trace.o $(OBJECTS) $(UARCH_DIR)/settings.tab.o $(UARCH_DIR)/lex.yy.o -o sim_trace -lfl -ly $(EXTRA_LIBS)

clean_bin:
	rm -f sim_trace


## cleaning
clean:
	-rm -rf $(OBJECTS) *~ sim_manifold *.d ./cache/*.o ./frontend/*.o ./*.o ./memory/*.o ./network/*.o ./processor/*.o ./processor/simple/*.o ./lex.yy.c ./settings.tab.* temperature_trace.txt power_trace.txt ./kernel/*.o

